import re

# read file
filepath = '3wishes.txt'
word_counter = {}
fp = open(filepath)
data = fp.read()

#delete non-alphabet string
data = re.sub(r'\W+',' ', data)
words = data.lower().split()
print(len(words))
##unique_words = set(words)
for wd in words:
    if wd not in word_counter:
        word_counter[wd] = 1
    else:
        word_counter[wd] = word_counter[wd] + 1

#print the word and corrensponding occurance number in the text
for (word, occurance) in word_counter.items():
    print('{:10} {:3}'.format(word, occurance))

# 思路是正确的，文件打开以后记得关闭，或者直接使用with语法。
